package com.mrd.modules.generator.service.impl;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.base.utils.StringUtils;
import com.mrd.modules.generator.dao.CodegenColumnDao;
import com.mrd.modules.generator.dao.CodegenTableDao;
import com.mrd.modules.generator.entity.CodegenColumnEntity;
import com.mrd.modules.generator.entity.CodegenTableEntity;
import com.mrd.modules.generator.service.CodegenTableService;
import com.mrd.modules.generator.utils.GenUtils;

@Service("codegenTableService")
public class CodegenTableServiceImpl implements CodegenTableService {
  @Autowired
  private CodegenTableDao codegenTableDao;
  @Autowired
  private CodegenColumnDao codegenColumnDao;

  @Override
  public CodegenTableEntity queryObject(String id) {
    return codegenTableDao.queryObject(id);
  }

  @Override
  public List<CodegenTableEntity> queryList(Map<String, Object> map) {
    return codegenTableDao.queryList(map);
  }

  @Override
  public int queryTotal(Map<String, Object> map) {
    return codegenTableDao.queryTotal(map);
  }

  @Override
  public void save(CodegenTableEntity codegenTable) {
    if (StringUtils.isEmpty(codegenTable.getId()))
      codegenTable.setId(StringUtils.randomUUID());
    codegenTableDao.save(codegenTable);
  }

  @Override
  public void update(CodegenTableEntity codegenTable) {
    codegenTableDao.update(codegenTable);
  }

  @Override
  public void delete(String id) {
    codegenTableDao.delete(id);
  }

  @Override
  public void deleteBatch(String[] ids) {
    codegenColumnDao.deleteBatchTableIds(ids);
    codegenTableDao.deleteBatch(ids);
  }

  /**
   * 高级查询
   * 
   * @param codegenTableEntity
   * @return
   */
  public List<CodegenTableEntity> queryPageList(CodegenTableEntity codegenTableEntity) {
    return codegenTableDao.queryPageList(codegenTableEntity);
  }

  /**
   * 高级代码生成
   * 
   * @author han
   * @param ids 主键数组
   * @return
   */
  public byte[] advGeneratorCode(String[] ids) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zip = new ZipOutputStream(outputStream);

    for (String id : ids) {
      // 查询表信息
      CodegenTableEntity table = codegenTableDao.queryObject(id);
      // 查询列信息
      Map<String, Object> columnSearchMap = new HashMap<String, Object>();
      columnSearchMap.put("tableId", table.getId());
      columnSearchMap.put("sidx", "sort");
      columnSearchMap.put("order", "desc");
      List<CodegenColumnEntity> columns = codegenColumnDao.queryList(columnSearchMap);
      // 生成代码
      GenUtils.generatorAdvaviceCode(table, columns, zip);;
    }
    IOUtils.closeQuietly(zip);
    return outputStream.toByteArray();
  }
}
